#include "pch.h"
#include <iostream>
#include "para.h"
#include "error.h"
#include <cstring>

using namespace std;

int check_path(char *path) {
    int len = strlen(path);
    if (len < 4 || path[len - 1] != 't' || path[len - 2] != 'x' || path[len - 3] != 't') {
        return -1;
    } else {
        return 0;
    }
}

int check_char(char *ch) {
    int len = strlen(ch);
    if (len > 1 || !isalpha(ch[0])) {
        return -1;
    } else {
        return 0;
    }
}

int parse_para(int argc, char *argv[], int &cal_type, int &head, int &tail,
               int &banned, int &loop, char **path) {
    cal_type = 0;
    head = 0;
    tail = 0;
    banned = 0;
    loop = 0;
    *path = NULL;

//    for(int i = 0;i < argc;i ++) printf("%s\n",argv[i]);
    if (argc == 1) {
        //缺少参数
        cerr << "Missing working parameters" << endl;
        return -lack_of_para;
    }
    for (int i = 1; i < argc; i++) {
        if (argv[i][0] != '-') {
            //参数格式不合法
            cerr << "Illegal parameter format" << endl;
            return -invalid_syntax;
        }
        switch (argv[i][1]) {
            case CAL_CHAIN_CNT:
                if (argc > 3) {
                    cerr << "-n Can only be used alone" << endl;
                    return -single_task;
                }
                if (cal_type > 0) {
                    //多重任务
                    cerr << "Repetition of assigned tasks" << endl;
                    return -dup_task;
                }
                if ((++i) >= argc) {
                    cerr << "Missing file path" << endl;
                    return -lack_of_path;
                }
                if (check_path(argv[i]) == 0) {
                    cal_type = 1;
                    *path = argv[i];
                } else {
                    cerr << "File path is not legal" << endl;
                    return -invalid_path;
                }
                break;

            case CAL_MAX_CHAIN_WORD:
                if (cal_type > 0) {
                    //多重任务
                    cerr << "Repetition of assigned tasks" << endl;
                    return -dup_task;
                }
                if ((++i) >= argc) {
                    cerr << "Missing file path" << endl;
                    return -lack_of_path;
                }
                if (check_path(argv[i]) == 0) {
                    cal_type = 2;
                    *path = argv[i];
                } else {
                    cerr << "File path is not legal" << endl;
                    return -invalid_path;
                }
                break;

            case CAL_MAX_CHAIN_CHAR:
                if (cal_type > 0) {
                    //多重任务
                    cerr << "Repetition of assigned tasks" << endl;
                    return -dup_task;
                }
                if ((++i) >= argc) {
                    cerr << "Missing file path" << endl;
                    return -lack_of_path;
                }
                if (check_path(argv[i]) == 0) {
                    cal_type = 3;
                    *path = argv[i];
                } else {
                    cerr << "File path is not legal" << endl;
                    return -invalid_path;
                }
                break;

            case SEL_CHAIN_HEAD:
                if (head > 0) {
                    //重复指定头
                    cerr << "Repeat the specified parameter -h" << endl;
                    return -dup_para;
                }
                if ((++i) >= argc) {
                    cerr << "Missing letter information" << endl;
                    return -lack_of_char;
                }
                if (check_char(argv[i]) == 0) {
                    head = (int) argv[i][0];
                } else {
                    cerr << "Lettering information is not legal" << endl;
                    return -invalid_char;
                }
                break;

            case SEL_CHAIN_TAIL:
                if (tail > 0) {
                    //重复指定尾
                    cerr << "Repeat the specified parameter -t" << endl;
                    return -dup_para;
                }
                if ((++i) >= argc) {
                    cerr << "Missing letter information" << endl;
                    return -lack_of_char;
                }
                if (check_char(argv[i]) == 0) {
                    tail = (int) argv[i][0];
                } else {
                    cerr << "Lettering information is not legal" << endl;
                    return -invalid_char;
                }
                break;

            case BAN_HEAD:
                if (banned > 0) {
                    //重复禁止头
                    cerr << "Repeat the specified parameter -j" << endl;
                    return -dup_para;
                }
                if ((++i) >= argc) {
                    cerr << "Missing letter information" << endl;
                    return -lack_of_char;
                }
                if (check_char(argv[i]) == 0) {
                    banned = (int) argv[i][0];
                } else {
                    cerr << "Lettering information is not legal" << endl;
                    return -invalid_char;
                }
                break;

            case ALLOW_LOOP:
                if (loop > 0) {
                    //重复允许环
                    cerr << "Repeat the specified parameters -r" << endl;
                    return -dup_para;
                }
                loop = 1;
                break;

            default:
                cerr << "Illegal parameter format" << endl;
                return -invalid_syntax;
        }
    }
    if (cal_type == 0) {
        cerr << "Missing working parameters" << endl;
        return -lack_of_para;
    }
    return 0;
}


